package dailytest;

import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.digest.HMac;
import org.bouncycastle.crypto.RuntimeCryptoException;


/**
 * @Auther: miyang
 * @Date: 2022/1/13
 * @Description:
 */
public class EncryptionUtil {

    private final static String CODE = "H33010201078";
    private final static String SECRET = "UFhvSP0DDS6foCI";
    private final static String CONTENT="{\n" +
            "                \"cainfo\": \"\",\n" +
            "                \"dev_no\": \"\",\n" +
            "                \"dev_safe_info\": \"\",\n" +
            "                \"fixmedins_code\": \"H33010201078\",\n" +
            "                \"fixmedins_name\": \"浙江大学医学院附属第一医院（浙江省第一医院）\",\n" +
            "                \"inf_time\": \"2022-10-14 14:13:32\",\n" +
            "                \"infno\": \"ZJ9671\",\n" +
            "                \"infver\": \"V1.0\",\n" +
            "                \"input\": {\n" +
            "                    \"data\": {\n" +
            "                        \"costCategory\": \"66\",\n" +
            "                        \"current\": 1,\n" +
            "                        \"medinsCode\": \"H33010201078\",\n" +
            "                        \"orgId\": 1,\n" +
            "                         \"list\":[\n" +
            "                             {\"ordDetlId\":\"12222\"}\n" +
            "                         ],\n" +
            "                        \"size\": 200,\n" +
            "                        \"userId\": 6568,\n" +
            "                        \"userName\": \"默认操作员\"\n" +
            "                    }\n" +
            "                },\n" +
            "                \"insuplc_admdvs\": \"330100\",\n" +
            "                \"mdtrtarea_admvs\": \"339900\",\n" +
            "                \"msgid\": \"H33010201078202209281613320004\",\n" +
            "                \"opter\": \"6568\",\n" +
            "                \"opter_name\": \"默认操作员\",\n" +
            "                \"opter_type\": \"1\",\n" +
            "                \"recer_sys_code\": \"MBS_LOCAL\",\n" +
            "                \"sign_no\": \"\",\n" +
            "                \"signtype\": \"SM3\"\n" +
            "    }";
    private final static String HOST = "10.249.83.126:80";


//    private final static String CODE = "H33010800954";
//    private final static String SECRET = "0y5RvzBzugxr8GF";
//    private final static String CONTENT="{\"authCode\":\"A0EF9C62-55FA-4B57-88E1-356C4643FA2D\",\"appCode\":\"447239C5-40CA-483B-9502-4F59A8749DBC\"}";




    public static String encrytSHA256(String content, String secret) {
        try {
            byte[] bytes = secret.getBytes("UTF-8");
            HMac hMac = SmUtil.hmacSm3(bytes);
            return hMac.digestHex(content).toUpperCase();
        } catch (Exception e) {
            throw new RuntimeCryptoException("加密异常");
        }
    }


    public static void main(String[] args) {
        String content = CONTENT;
        long currentTimeMillis = System.currentTimeMillis();
        String signature = currentTimeMillis + ":" + encrytSHA256(currentTimeMillis + "\n" + content,SECRET);
        String s = initCurl(signature, content, "/tps-local-bd/intf/api/accessToken/getAccessHospToken");
        System.out.println("s = " + s);

    }


    private static String initCurl(String qm, String param, String url) {
        StringBuffer sb = new StringBuffer();
        sb.append("curl -H \"Content-Type: application/json\" -H \"x-ca-key: " + CODE + "\" -H \"x-ca-signature:");
        sb.append(qm + "\"  -X POST -d '");
        sb.append(param + "' " + HOST + url);
        return sb.toString();
    }
}
